package com.yuju.controller.group;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.OwnerCustomReturnCodeEnum;
import com.yuju.group.model.OwnerCustomModel;
import com.yuju.group.service.IOwnerCustomService;
import com.yuju.util.Utils;
import com.yuju.vo.group.OwnerCustomVO;

/**
 * 类名：OwnerCustomController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月24日 下午4:39:00  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("ownerCustom")
public class OwnerCustomController extends BaseController<OwnerCustomVO, OwnerCustomModel> {
	
	@Autowired
	private IOwnerCustomService ownerCustomService;
	
	@RequestMapping("validateName")
	@ResponseBody
	public Result validateName(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		ownerCustomVO.setHotelId(null);
		boolean validateName = ownerCustomService.validateName(convert(ownerCustomVO));
		Result result = new Result();
		if(validateName){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("listOwner")
	@ResponseBody
	public Result listOwner(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		List<OwnerCustomModel> list = ownerCustomService.list(convert(ownerCustomVO));
		Result result = new Result();
		if(Utils.isNotNull(list)){
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		ownerCustomVO.setHotelId(null);
		Map<String, String> fail = FieldValidator.validateAll(ownerCustomVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageList = ownerCustomService.pageList(convert(ownerCustomVO), ownerCustomVO.getPage(), ownerCustomVO.getPageSize());
		if(Utils.isNotNull(pageList)){
			result.setPageModel(pageList);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(ownerCustomVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		ownerCustomVO.setHotelId(null);
		Result result = new Result();
		OwnerCustomModel find = ownerCustomService.find(convert(ownerCustomVO));
		if(Utils.isNotNull(find)){
			result.setData(find);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(ownerCustomVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int i = ownerCustomService.updateSelective(convert(ownerCustomVO));
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_UPDATE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_UPDATE_FAILURE.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(ownerCustomVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int save = ownerCustomService.save(convert(ownerCustomVO));
		if(save>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_SAVE_SUCCESS.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_SAVE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_SAVE_FAILURE.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj){
		OwnerCustomVO ownerCustomVO = obj.toJavaObject(OwnerCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(ownerCustomVO, "delete", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int del = ownerCustomService.del(ownerCustomVO.getId());
		if(del>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_DEL_SUCCESS.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_DEL_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(OwnerCustomReturnCodeEnum.OWNERCUSTOM_DEL_FAILURE.getCode());
			result.setReturnMsg(OwnerCustomReturnCodeEnum.OWNERCUSTOM_DEL_FAILURE.getDesc());
		}
		return result;
	}
}
